package com.iot08.api.config.sms;

import com.iot08.api.service.JdyunSmsService;
import com.iot08.api.service.sms.IProviderJdySmsService;
import com.jdcloud.sdk.auth.CredentialsProvider;
import com.jdcloud.sdk.auth.StaticCredentialsProvider;
import com.jdcloud.sdk.http.HttpRequestConfig;
import com.jdcloud.sdk.http.Protocol;
import com.jdcloud.sdk.service.sms.client.SmsClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cwj
 * @description
 * @date 2025/6/3 11:40
 **/
@Configuration(proxyBeanMethods = false)
public class JdySdkSmsAutoConfiguration {
    
    /**
     * @description 读京东云的配置
     * @author cwj
     * @param []
     * @return com.iot08.api.config.sms.JdySdkSmsClientConfig
     * @date @time 2025/6/3 14:13
     **/
    @Bean
    @RefreshScope
    @ConfigurationProperties(prefix = "jdyun.sms")
    public JdySdkSmsClientConfig jdySmsConfig() {
        return new JdySdkSmsClientConfig();
    }


    /**
     * @description 构建京东云发送短信的客户端
     * @author cwj
     * @param [smsConfig]
     * @return com.jdcloud.sdk.service.sms.client.SmsClient
     * @date @time 2025/6/3 13:57
     **/
    @Bean
    public SmsClient createClient(JdySdkSmsClientConfig jdySdkSmsClientConfig) {
        CredentialsProvider credentialsProvider = new StaticCredentialsProvider(jdySdkSmsClientConfig.getAccessKeyId(), jdySdkSmsClientConfig.getAccessKeySecret());
        return SmsClient.builder()
                .credentialsProvider(credentialsProvider)
                .httpRequestConfig(new HttpRequestConfig.Builder().protocol(Protocol.HTTPS).build()) //默认为HTTPS
                .build();
    }


    /**
     * @description 构建京东云发送短信的对象
     * @author cwj
     * @param [smsClient]
     * @return com.iot08.api.service.sms.IProviderJdySmsService
     * @date @time 2025/6/3 14:49
     **/
    @Bean
    public IProviderJdySmsService bulidJdyunSmsService(SmsClient smsClient)  {
        return new JdyunSmsService(smsClient);
    }
    

}
